﻿//********************************************************************************//
//创建日期：<创建日期，2016-03-17>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：预编制计划
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using System.Text;
using Web.Class;
using DevExpress.Web;

namespace Web.Pages.YongShui.JiHuaGL
{
    public partial class YuBianZJH : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集(计划执行情况)
        /// </summary>
        private DataTable dtJiHuaZXQK
        {
            get
            {
                return ViewState["dtJiHuaZXQK"] as DataTable;
            }
            set
            {
                ViewState["dtJiHuaZXQK"] = value;
            }
        }

        /// <summary>
        /// 保存结果集(特殊用户)
        /// </summary>
        private DataTable dtTeShuYH
        {
            get
            {
                return ViewState["dtTeShuYH"] as DataTable;
            }
            set
            {
                ViewState["dtTeShuYH"] = value;
            }
        }

        /// <summary>
        /// 保存结果集(预测分析)
        /// </summary>
        private DataTable dtYuCheFX
        {
            get
            {
                return ViewState["dtYuCheFX"] as DataTable;
            }
            set
            {
                ViewState["dtYuCheFX"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 计划参数
        /// </summary>
        YS_JH_YUBIANZJHPZ _YS_JH_YUBIANZJHPZ = new YS_JH_YUBIANZJHPZ();

        /// <summary>
        /// 计划参数BLL
        /// </summary>
        YS_JH_YUBIANZJHPZBLL _YS_JH_YUBIANZJHPZBLL = new YS_JH_YUBIANZJHPZBLL();

        /// <summary>
        /// 特殊用户对象
        /// </summary>
        YS_JH_BUDUIFLSPH _YS_JH_BUDUIFLSPH = new YS_JH_BUDUIFLSPH();

        /// <summary>
        /// 特殊用户BLL
        /// </summary>
        YS_JH_BUDUIFLSPHBLL _YS_JH_BUDUIFLSPHBLL = new YS_JH_BUDUIFLSPHBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
            }
                //this.CreateDataSource("0");
                this.CreateDataSource("1");
                //this.CreateDataSource("3");
            //用于执行导出
            this.ScriptManager1.RegisterPostBackControl(this.btnActPerformExport);
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            int delID = Convert.ToInt32(e.CommandArgument);
            this._YS_JH_BUDUIFLSPH = this._YS_JH_BUDUIFLSPHBLL.SelectYS_JH_BUDUIFLSPH(delID);
            this._YS_JH_BUDUIFLSPH.I_JLZT = -1;
            _SYS_LOGBLL.Log<YS_JH_BUDUIFLSPH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "特殊用户", OperateState.Delete, _YS_JH_BUDUIFLSPH, null);
            if (this._YS_JH_BUDUIFLSPHBLL.InsertOrUpdate(_YS_JH_BUDUIFLSPH))
            {
                
                PageTools.AlertMsg(this, "操作成功！");
                this.CreateDataSource("1");
            }
        }

        ///// <summary>
        ///// 预编制计划导入
        ///// </summary>
        //protected void tab3btnInport_Click(object sender, EventArgs e)
        //{
        //    //判断来年计划是否已经下达
        //    YS_JH_YUANJIHUABLL bllT = new YS_JH_YUANJIHUABLL();
        //    DataRowCollection drc = bllT.SelectYS_JH_YUANJIHUAsDynamic("I_Y=" + (int.Parse(this.tab3txtY.Text)).ToString(), null).Rows;
        //    if (drc.Count > 0)
        //    {
        //        PageTools.AlertMsg(this, "来年计划已下达,不允许再进行本次操作。");
        //        return;
        //    }

        //}

        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "tab1export")
            {
                if (this.tab1gvList1.VisibleRowCount == 0)
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = "没有可供导出的数据";
                    e.Result = cr.ToString();
                }
                else
                {
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "tab2export")
            {
                if (this.tab2gvList1.VisibleRowCount == 0)
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = "没有可供导出的数据";
                    e.Result = cr.ToString();
                }
                else
                {
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "tab3export")
            {
                cr.IsSuccess = true;
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "tab4export")
            {
                if (this.tab4gvList1.VisibleRowCount == 0)
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = "没有可供导出的数据";
                    e.Result = cr.ToString();
                }
                else
                {
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "tab3inport")
            {
                YS_JH_YUANJIHUABLL bllT = new YS_JH_YUANJIHUABLL();
                DataRowCollection drc = bllT.SelectYS_JH_YUANJIHUAsDynamic("I_Y=" + (int.Parse(this.tab3txtY.Text)).ToString(), null).Rows;
                if (drc.Count > 0)
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = "来年计划已下达,不允许再进行本次操作。";
                    e.Result = cr.ToString();
                }
                else
                {
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "validate_tab2Add")
            {
                string sheshuidxdm = null;
                if (this.tab2cmbBZ.SelectedIndex == 0)
                {
                    sheshuidxdm = this.tab2cmbBZ.Value.ToString();
                    cr.Msg = "不存在与输入的用户号相对应的福利用户！";
                }
                else if (this.tab2cmbBZ.SelectedIndex == 1)
                {
                    sheshuidxdm = this.tab2cmbBZ.Value.ToString();
                    cr.Msg = "不存在与输入的用户号相对应的部队用户！";
                }
                else
                {
                    cr.Msg = "不存在与输入的用户号相对应的用户！";
                }
                //判断当前列表中是否已经存在该用户
                int index = PageTools.SearchRecordIndex(this.tab2gvList1, "S_YONGHUH", this.tab2txtYonghuh.Text, true);
                if (index > -1)
                {
                    cr.Msg = "已存在！";
                    cr.SpecialIndetity = "-2";
                    cr.AddValue(index);
                    e.Result = cr.ToString();
                    return;
                }
                //判断用户合法性
                if (_spHelper.P_YS_GETTESHUYHXXFROMYHXX(this.tab2txtYonghuh.Text, sheshuidxdm).Tables[0].Rows.Count == 0)
                {
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
                cr.IsSuccess = true;
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "tab2Save")
            {
                //执行保存  this.tab2cmbBZ.SelectedIndex+1与福利:1 部队:2 未完成水平衡:3 匹配
                _spHelper.P_YS_TESHUYHSAVE(this.tab2txtYonghuh.Text, Convert.ToInt32(this.tab2cmbY.Value), Convert.ToInt32(this.tab2cmbBZ.SelectedIndex + 1), 0);
                cr.IsSuccess = true;
                cr.AddValue(this.tab2txtYonghuh.Text);
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "tab2Rebuild")
            {
                _spHelper.P_YSJH_TESHUYHREBUILD(Convert.ToInt32(this.tab2cmbY.Value));
                cr.IsSuccess = true;
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "rebuild")
            {
                //重新生成预编制参数
                this._YS_JH_YUBIANZJHPZBLL.UpdateYuBianZJHPZ(this.tab3cmbBanShiCFangA.Value.ToString(), this.tab3txtBANSHICXX.Text, this.tab3cmbBenBuFangA.Value.ToString(), this.tab3txtBENBUXX.Text, this.tab3txtBENNIANQ.Text, this.tab3txtCHAOJIHTJD.Text,
                                            this.tab3txtCHAOJIHTJG.Text, this.tab3txtCHAOJIHTJZ.Text, this.tab3txtCHAOJIHTJSX.Text, this.tab3txtCHAOYONGLSX.Text, this.tab3txtCHAOYONGLTJ.Text, this.tab3txtCHAOYONGLYS.Text,
                                            this.tab3txtCHAOYONGLYZ.Text, this.tab3txtFULIWCL.Text, this.tab3txtFULIWCLXX.Text, this.tab3txtJIAJIABL.Text, this.tab3txtJINNIANQ.Text, this.tab3txtQIANNIANQ.Text,
                                            this.tab3txtQUNIANQ.Text, this.tab3txtSHANGNIANQ.Text, this.tab3txtWANCHENGLTJ.Text, this.tab3txtWANCHENGLXX.Text, this.tab3txtWANCHENGLYS.Text, this.tab3txtWANCHENGLYZ.Text,
                                            this.tab3txtY.Text, this.tab3txtM.Text, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                //得到近三年实用
                string rsno = "-1";
                string rsmsg = "";
                if (int.Parse(this.tab3txtM.Text) > 0)
                    _spHelper.P_YSJH_GETNEAR3YEARSFACTUSE(int.Parse(this.tab3txtY.Text), 0, out rsno, out rsmsg);
                _spHelper.P_YSJH_GETNEAR3YEARSFACTUSE(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), out rsno, out rsmsg);
                DBResult rs = new DBResult { RSNO = rsno, RSMSG = rsmsg };
                if (rs.IsSuccess)
                {
                    //执行方案
                    int iway = -1;
                    if (this.tab3cmbBenBuFangA.SelectedIndex == 0)
                    {
                        iway = 0;
                        _spHelper.P_YSJH_YUBIANZHIJHFA1(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), null, this.tab3txtFULIWCLXX.Text, this.tab3txtFULIWCL.Text
                            , this.tab3txtBANSHICXX.Text, this.tab3txtBENBUXX.Text, this.tab3txtJIAJIABL.Text, this.tab3txtWANCHENGLYZ.Text, this.tab3txtWANCHENGLTJ.Text
                            , this.tab3txtWANCHENGLYS.Text, this.tab3txtWANCHENGLXX.Text, this.tab3txtCHAOYONGLYZ.Text, this.tab3txtCHAOYONGLTJ.Text, this.tab3txtCHAOYONGLYS.Text
                            , this.tab3txtCHAOYONGLSX.Text, this.tab3cbDingE.Checked ? 1 : 0, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                    }
                    else if (this.tab3cmbBenBuFangA.SelectedIndex == 1)
                    {
                        iway = 1;
                        _spHelper.P_YSJH_YUBIANZHIJHFA2(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), null, this.tab3txtFULIWCLXX.Text, this.tab3txtFULIWCL.Text
                            , this.tab3txtBANSHICXX.Text, this.tab3txtBENBUXX.Text, this.tab3txtJIAJIABL.Text, int.Parse(this.tab3txtJINNIANQ.Text), int.Parse(this.tab3txtQUNIANQ.Text)
                        , int.Parse(this.tab3txtQIANNIANQ.Text), int.Parse(this.tab3txtBENBUXX.Text), int.Parse(this.tab3txtSHANGNIANQ.Text), this.tab3cbDingE.Checked ? 1 : 0, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                    }
                    else if (this.tab3cmbBenBuFangA.SelectedIndex == 2)
                    {
                        iway = 2;
                        _spHelper.P_YSJH_YUBIANZHIJHFA3(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), this.tab3txtFULIWCLXX.Text, this.tab3txtFULIWCL.Text
                            , this.tab3txtBANSHICXX.Text, this.tab3txtBENBUXX.Text, this.tab3txtJIAJIABL.Text, this.tab3cbCFLY.Checked ? 1 : 0, int.Parse(this.tab3txtCHAOJIHTJD.Text), int.Parse(this.tab3txtCHAOJIHTJZ.Text)
                        , int.Parse(this.tab3txtCHAOJIHTJSX.Text), int.Parse(this.tab3txtCHAOJIHTJG.Text), this.tab3cbDingE.Checked ? 1 : 0, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                    }
                    if (this.tab3cmbBanShiCFangA.SelectedIndex == 0 && iway != 0)
                    {
                        _spHelper.P_YSJH_YUBIANZHIJHFA1(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), null, this.tab3txtFULIWCLXX.Text, this.tab3txtFULIWCL.Text
                            , this.tab3txtBANSHICXX.Text, this.tab3txtBENBUXX.Text, this.tab3txtJIAJIABL.Text, this.tab3txtWANCHENGLYZ.Text, this.tab3txtWANCHENGLTJ.Text
                            , this.tab3txtWANCHENGLYS.Text, this.tab3txtWANCHENGLXX.Text, this.tab3txtCHAOYONGLYZ.Text, this.tab3txtCHAOYONGLTJ.Text, this.tab3txtCHAOYONGLYS.Text
                            , this.tab3txtCHAOYONGLSX.Text, this.tab3cbDingE.Checked ? 1 : 0, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                    }
                    else if (this.tab3cmbBanShiCFangA.SelectedIndex == 1 && iway != 1)
                    {
                        _spHelper.P_YSJH_YUBIANZHIJHFA2(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), null, this.tab3txtFULIWCLXX.Text, this.tab3txtFULIWCL.Text
                            , this.tab3txtBANSHICXX.Text, this.tab3txtBENBUXX.Text, this.tab3txtJIAJIABL.Text, int.Parse(this.tab3txtJINNIANQ.Text), int.Parse(this.tab3txtQUNIANQ.Text)
                        , int.Parse(this.tab3txtQIANNIANQ.Text), int.Parse(this.tab3txtBENBUXX.Text), int.Parse(this.tab3txtSHANGNIANQ.Text), this.tab3cbDingE.Checked ? 1 : 0, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                    }
                    else if (this.tab3cmbBenBuFangA.SelectedIndex == 2 && iway != 2)
                    {
                        _spHelper.P_YSJH_YUBIANZHIJHFA3(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), this.tab3txtFULIWCLXX.Text, this.tab3txtFULIWCL.Text
                            , this.tab3txtBANSHICXX.Text, this.tab3txtBENBUXX.Text, this.tab3txtJIAJIABL.Text, this.tab3cbCFLY.Checked ? 1 : 0, int.Parse(this.tab3txtCHAOJIHTJD.Text), int.Parse(this.tab3txtCHAOJIHTJZ.Text)
                        , int.Parse(this.tab3txtCHAOJIHTJSX.Text), int.Parse(this.tab3txtCHAOJIHTJG.Text), this.tab3cbDingE.Checked ? 1 : 0, LoginInfo.CurrentUser.Account, BusinessTool.GetDataBaseDateTime());
                    }
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                }
                else
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    cr.Msg = rsmsg;
                }
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "quedingfa")
            {
                //判断是否可以执行确定计划
                string rsno = "-1";
                string rsmsg = "";
                _spHelper.P_YSJH_QUEDINGJHPD(int.Parse(this.tab3txtY.Text) + 1, out rsno, out rsmsg);
                if (rsno == "-1")
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = rsmsg;
                    e.Result = cr.ToString();
                    return;
                }
                //更新本部,办事处确定方案及当前步骤
                this._YS_JH_YUBIANZJHPZBLL.UpdateQDFN(this.tab3cmbBenBuFangA.SelectedIndex + 1, this.tab3cmbBanShiCFangA.SelectedIndex + 1, this.tab3txtY.Text);
                //执行确定计划
                _spHelper.P_YSJH_YUBIANZHIJHFAQD(int.Parse(this.tab3txtY.Text) + 1, this.tab3cmbBenBuFangA.SelectedIndex + 1, this.tab3cmbBanShiCFangA.SelectedIndex + 1, out rsno, out rsmsg);
                cr.IsSuccess = true;
                cr.IsAlertMsg = true;
                cr.Msg = "操作成功！";
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "exportNear3yers")
            {
                //得到近三年实用
                string rsno = "-1";
                string rsmsg = "";
                _spHelper.P_YSJH_GETNEAR3YEARSFACTUSE(int.Parse(this.tab3txtY.Text), int.Parse(this.tab3txtM.Text), out rsno, out rsmsg);
                if (rsno == "1")
                {
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "DelUser")
            {
                int delID = Convert.ToInt32(this.hdCollection.Get("DelUserID").ToString());
                this._YS_JH_BUDUIFLSPH = this._YS_JH_BUDUIFLSPHBLL.SelectYS_JH_BUDUIFLSPH(delID);
                this._YS_JH_BUDUIFLSPH.I_JLZT = -1;
                _SYS_LOGBLL.Log<YS_JH_BUDUIFLSPH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "特殊用户", OperateState.Delete, _YS_JH_BUDUIFLSPH, null);
                if (this._YS_JH_BUDUIFLSPHBLL.InsertOrUpdate(_YS_JH_BUDUIFLSPH))
                {
                    //CreateDataSource("1");
                    e.Result = "操作成功！";
                }
                else e.Result = "操作失败！";
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerformExport_Click(object sender, EventArgs e)
        {
            if (this.hdCollection.Get("type").ToString() == "exportNear3yers")
            {
                //获取近三年水量
                DataTable dtTemp = _spHelper.P_YSJH_GETNEAR3YEARSFACTALL().Tables[0];
                dtTemp.Columns.RemoveAt(0);
                DataTable dt = new DataTable();
                dt.Columns.Add("用户号");
                dt.Columns.Add("年");
                dt.Columns.Add("一月");
                dt.Columns.Add("二月");
                dt.Columns.Add("三月");
                dt.Columns.Add("四月");
                dt.Columns.Add("五月");
                dt.Columns.Add("六月");
                dt.Columns.Add("七月");
                dt.Columns.Add("八月");
                dt.Columns.Add("九月");
                dt.Columns.Add("十月");
                dt.Columns.Add("十一月");
                dt.Columns.Add("十二月");
                dt.Columns.Add("用水价");
                dt.Columns.Add("月");
                for (int i = 0; i < dtTemp.Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["用户号"] = "'" + dtTemp.Rows[i]["S_YONGHUH"].ToString();
                    dr["年"] = dtTemp.Rows[i]["I_Y"];
                    dr["一月"] = dtTemp.Rows[i]["I_M1"];
                    dr["二月"] = dtTemp.Rows[i]["I_M2"];
                    dr["三月"] = dtTemp.Rows[i]["I_M3"];
                    dr["四月"] = dtTemp.Rows[i]["I_M4"];
                    dr["五月"] = dtTemp.Rows[i]["I_M5"];
                    dr["六月"] = dtTemp.Rows[i]["I_M6"];
                    dr["七月"] = dtTemp.Rows[i]["I_M7"];
                    dr["八月"] = dtTemp.Rows[i]["I_M8"];
                    dr["九月"] = dtTemp.Rows[i]["I_M9"];
                    dr["十月"] = dtTemp.Rows[i]["I_M10"];
                    dr["十一月"] = dtTemp.Rows[i]["I_M11"];
                    dr["十二月"] = dtTemp.Rows[i]["I_M12"];
                    dr["用水价"] = dtTemp.Rows[i]["N_YSJ"];
                    dr["月"] = dtTemp.Rows[i]["I_MONTH"];
                    dt.Rows.Add(dr);
                }

                PageTools.CreateExcel2(dt, "近三年实用水量.xls");
            }
            else if (this.hdCollection.Get("type").ToString() == "tab1export")
            {
                //DataTable dt = new DataTable();
                //for (int i = 0; i < this.tab1gvList1.Columns.Count; i++)
                //{
                //    dt.Columns.Add(this.tab1gvList1.Columns[i].Caption);
                //}
                //for (int i = 0; i < this.tab1gvList1.VisibleRowCount; i++)
                //{
                //    DataRow dr = dt.NewRow();
                //    dr[0] = this.tab1gvList1.GetRowValues(i, "部门");
                //    dr[1] = this.tab1gvList1.GetRowValues(i, "XXXX年计划量");
                //    dr[2] = this.tab1gvList1.GetRowValues(i, "XXXX年实用量");
                //    dr[3] = this.tab1gvList1.GetRowValues(i, "预测超计划金额");
                //    dr[4] = this.tab1gvList1.GetRowValues(i, "XXXX年考核计划");
                //    dr[5] = this.tab1gvList1.GetRowValues(i, "XXXX年X至X月实用量");
                //    dr[6] = this.tab1gvList1.GetRowValues(i, "实际超计划金额");
                //    dr[7] = this.tab1gvList1.GetRowValues(i, "实际加价金额");
                //    dr[8] = this.tab1gvList1.GetRowValues(i, "推测全年超计划金额");
                //    dr[9] = this.tab1gvList1.GetRowValues(i, "推测全年加价金额");
                //    dr[10] = this.tab1gvList1.GetRowValues(i, "推测剩余月超计划金额");
                //    dr[11] = this.tab1gvList1.GetRowValues(i, "未开帐月超计划金额");
                //    dt.Rows.Add(dr);
                //}                
                //PageTools.(this.CreateDataSource("0"), "计划执行情况.xls");
            }
            else if (this.hdCollection.Get("type").ToString() == "tab2export")
            {
                //DataTable dt = new DataTable();
                //dt.Columns.Add("用户号");
                //dt.Columns.Add("用户名");
                //dt.Columns.Add("所属局");
                //dt.Columns.Add("用水性质");
                //for (int i = 0; i < this.tab2gvList1.VisibleRowCount; i++)
                //{
                //    DataRow dr = dt.NewRow();
                //    dr["用户号"] = "&nbsp;"+this.tab2gvList1.GetRowValues(i, "S_YONGHUH");
                //    dr["用户名"] = this.tab2gvList1.GetRowValues(i, "S_YONGHUM");
                //    dr["所属局"] = this.tab2gvList1.GetRowValues(i, "JU");
                //    dr["用水性质"] = this.tab2gvList1.GetRowValues(i, "YONGSHUIXZ");
                //    dt.Rows.Add(dr);
                //}
                //PageTools.(this.CreateDataSource("1"), this.tab2cmbBZ.Text + ".xls");
            }
            else if (this.hdCollection.Get("type").ToString() == "tab3export")
            {
                //DataTable dtTemp = _spHelper.P_YSJH_YUBIANZHIJHDC(int.Parse(this.tab3txtY.Text) + 1, 1, 3).Tables[0];
                //PageTools.CreateExcel(this.CreateDataSource("2"), "预编制计划_" + (int.Parse(this.tab3txtY.Text) + 1) + ".xls");
            }
            else if (this.hdCollection.Get("type").ToString() == "tab4export")
            {
            //    DataTable dt = new DataTable();
            //    dt.Columns.Add("部门");
            //    dt.Columns.Add("计划量");
            //    dt.Columns.Add("计划增减");
            //    dt.Columns.Add("实用量");
            //    dt.Columns.Add("超计划金额");
            //    dt.Columns.Add("加价户次");
            //    dt.Columns.Add("加价户数");
            //    dt.Columns.Add("增加计划户");
            //    dt.Columns.Add("减少计划户");
            //    for (int i = 0; i < this.tab4gvList1.VisibleRowCount; i++)
            //    {
            //        DataRow dr = dt.NewRow();
            //        dr["部门"] = this.tab4gvList1.GetRowValues(i, "S_ZHANDIAN");
            //        dr["计划量"] = this.tab4gvList1.GetRowValues(i, "I_PLAN");
            //        dr["计划增减"] = this.tab4gvList1.GetRowValues(i, "I_PLANZJ");
            //        dr["实用量"] = this.tab4gvList1.GetRowValues(i, "I_SYSL");
            //        dr["超计划金额"] = this.tab4gvList1.GetRowValues(i, "N_CJHJE");
            //        dr["加价户次"] = this.tab4gvList1.GetRowValues(i, "I_JJHC");
            //        dr["加价户数"] = this.tab4gvList1.GetRowValues(i, "I_JJHS");
            //        dr["增加计划户"] = this.tab4gvList1.GetRowValues(i, "I_ZPLANHS");
            //        dr["减少计划户"] = this.tab4gvList1.GetRowValues(i, "I_JPLANHS");
            //        dt.Rows.Add(dr);
            //    }
            //    PageTools.CreateExcel(dt, this.tab4cmbType.Text + "_" + this.tab4txtY.Text + ".xls");
            }
        }
        private DataTable CreateDataSource(string type)
        {
            DataTable dt = new DataTable();
            if(type=="0")
            {
                int y = int.Parse(this.tab1txtY.Text);
                this.tab1gvList1.Columns["XXXX年计划量"].Caption = y.ToString() + "年下达计划量";
                this.tab1gvList1.Columns["XXXX年实用量"].Caption = (y - 1).ToString() + "年实用量";
                this.tab1gvList1.Columns["XXXX年考核计划"].Caption = y.ToString() + "年考核计划量";
                this.tab1gvList1.Columns["XXXX年X至X月实用量"].Caption = y.ToString() + "年" + "1~" + this.tab1txtM.Text + "月实用量";
                
                //判断是否已开账
                DataSet ds = BusinessTool.GetTextAndValue("YS_ZW_YINGYEZ", "I_Y", "I_M", null, "I_JLZT=0 AND I_Y=" + this.tab1txtY.Text + " AND I_M=" + this.tab1txtM.Text);
                int jjm = -1;
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    jjm = int.Parse(this.tab1txtM.Text);
                }
                else
                {
                    jjm = this.tab1txtM.Text=="1"?12:(int.Parse(this.tab1txtM.Text) - 1);
                }
                this.tab1gvList1.Columns["实际加价金额"].Caption = "1~" + jjm + "月实际加价额";
                dt = SearchSpHelper.P_YSJH_JIHUAZXQK(int.Parse(this.tab1txtY.Text), int.Parse(this.tab1txtM.Text), int.Parse(this.tab1txtJIAJIABL.Text)).Tables[0];
                if (dt.Rows.Count == 1)
                {
                    dt = null;
                }
            }
            else if(type=="1")
            {
                dt = _spHelper.P_YS_GETTESHUYONGHU(this.tab2cmbBZ.SelectedIndex + 1, Convert.ToInt32(this.tab2cmbY.Value), "i_y,i_bz,s_yonghuh").Tables[0];
                this.tab2gvList1.DataSource = dt; 
                this.tab2gvList1.DataBind();
                this.tab2lblRowCount.Text = "共有" + this.tab2gvList1.VisibleRowCount + "条记录";
            }
            else if(type=="2")
            {
                dt = _spHelper.P_YSJH_YUBIANZHIJHDC(int.Parse(this.tab3txtY.Text) + 1, 1, 3).Tables[0];
            }
            else if (type == "3")
            {
                dt=LoadDataYuCheFX();
            }
            return dt;
        
        }
        /// <summary>
        /// 刷新计划执行情况
        /// </summary>
        protected void tab1gvList1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            this.tab1gvList1.DataSource = this.CreateDataSource("0");
            this.tab1gvList1.DataBind();
        }



        /// <summary>
        /// 特殊用户列表刷新
        /// </summary>
        protected void tab2gvList1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            this.CreateDataSource("1");
            if (!string.IsNullOrEmpty(e.Parameters))
            {
                int index = PageTools.SearchRecordIndex(this.tab2gvList1, "S_YONGHUH", e.Parameters, true);
                this.tab2gvList1.JSProperties.Add("cpIndex", index);
            }
            this.tab2gvList1.JSProperties.Add("cpCount", this.tab2gvList1.VisibleRowCount);
        }

        /// <summary>
        /// 刷新预测分析列表
        /// </summary>
        protected void tab4gvList1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadDataYuCheFX();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件及数据
        /// </summary>
        private void InitControl()
        {
            //获取水量年月
            int ym = Convert.ToInt32(_spHelper.P_YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()).Tables[0].Rows[0][0]);
            //在前台记录下水量年用于前台判断输出年月不能超出水量年
            this.hdCollection.Set("ym", ym);
            int y = ym / 100;
            int m = ym % 100;
            //获取计划参数
            DataRowCollection drc = this._YS_JH_YUBIANZJHPZBLL.SelectYS_JH_YUBIANZJHPZsDynamic(string.Format("I_Y={0}", (Convert.ToInt32(ym) / 100)), null).Rows;
            bool hasNowYear;
            if (drc.Count == 0)
            {
                hasNowYear = false;
                drc = this._YS_JH_YUBIANZJHPZBLL.SelectYS_JH_YUBIANZJHPZsDynamic(string.Format("I_Y={0}", (Convert.ToInt32(ym) / 100) - 1), null).Rows;
            }
            else
            {
                hasNowYear = true;
            }
            //加载计划执行情况
            this.tab1txtY.Text = (ym / 100).ToString();
            this.tab1txtM.Text = (ym % 100).ToString();
            this.tab1txtJIAJIABL.Text = drc.Count == 0 ? "" : drc[0]["S_JIAJIABL"].ToString();
            //记录加价倍率,本部选用及办事处选用方案,第四选项卡要用
            this.hdCollection.Set("jjbl", this.tab1txtJIAJIABL.Text);
            this.hdCollection.Set("bbxy", drc.Count == 0 ? "" : drc[0]["S_BENBUFN"]);
            this.hdCollection.Set("bscxy", drc.Count == 0 ? "" : drc[0]["S_BANSHICFN"]);
            this.tab1gvList1.Columns["XXXX年计划量"].Caption = y.ToString() + "年下达计划量";
            this.tab1gvList1.Columns["XXXX年实用量"].Caption = (y - 1).ToString() + "年实用量";
            this.tab1gvList1.Columns["XXXX年考核计划"].Caption = y.ToString() + "年考核计划量";
            this.tab1gvList1.Columns["XXXX年X至X月实用量"].Caption = y.ToString() + "年" + "1-" + m.ToString() + "月实用量";
            //加载特殊用户录入
            for (int i = DateTime.Now.Year+2; i > y - 50; i--)
            {
                this.tab2cmbY.Items.Add(i.ToString(), i);
            }
            this.tab2cmbY.SelectedIndex = 2;
            //加载特殊用户类型
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(this.tab2cmbBZ, manger.Instance.FindWordsDataTableByBelongID(WordsStruct.BelongID.特殊用户涉水对象代码));
            this.tab2cmbBZ.SelectedIndex = 0;
            //加载预编制计划参数
            this.tab3lblTip1.Text = "月预设" + (y + 1).ToString() + "年计划";
            this.tab3lblTip2.Text = "以" + y.ToString() + "年考核计划为计划基准";
            this.tab3lblTip3.Text = (y + 1).ToString() + "计划=" + y.ToString() + "实用×";
            this.tab3lblTip4.Text = "%+" + (y - 1).ToString() + "实用×";
            this.tab3lblTip5.Text = "%+" + (y - 2).ToString() + "实用×";
            this.tab3txtY.Text = y.ToString();
            this.tab3txtM.Text = m.ToString();
            this.tab3txtFULIWCLXX.Text = drc.Count == 0 ? "" : drc[0]["S_FULIWCLXX"].ToString();
            this.tab3txtFULIWCL.Text = drc.Count == 0 ? "" : drc[0]["S_FULIWCL"].ToString();
            this.tab3txtBANSHICXX.Text = drc.Count == 0 ? "" : drc[0]["S_BANSHICXX"].ToString();
            this.tab3txtBENBUXX.Text = drc.Count == 0 ? "" : drc[0]["S_BENBUXX"].ToString();
            this.tab3txtJIAJIABL.Text = drc.Count == 0 ? "" : drc[0]["S_JIAJIABL"].ToString();
            this.tab3txtWANCHENGLYZ.Text = drc.Count == 0 ? "" : drc[0]["S_WANCHENGLYZ"].ToString();
            this.tab3txtWANCHENGLTJ.Text = drc.Count == 0 ? "" : drc[0]["S_WANCHENGLTJ"].ToString();
            this.tab3txtFULIWCLXX.Text = drc.Count == 0 ? "" : drc[0]["S_FULIWCLXX"].ToString();
            this.tab3txtWANCHENGLYS.Text = drc.Count == 0 ? "" : drc[0]["S_WANCHENGLYS"].ToString();
            this.tab3txtWANCHENGLXX.Text = drc.Count == 0 ? "" : drc[0]["S_WANCHENGLXX"].ToString();
            this.tab3txtCHAOYONGLYZ.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOYONGLYZ"].ToString();
            this.tab3txtCHAOYONGLTJ.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOYONGLTJ"].ToString();
            this.tab3txtCHAOYONGLYS.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOYONGLYS"].ToString();
            this.tab3txtCHAOYONGLSX.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOYONGLSX"].ToString();
            this.tab3txtJINNIANQ.Text = drc.Count == 0 ? "" : drc[0]["S_JINNIANQ"].ToString();
            this.tab3txtQUNIANQ.Text = drc.Count == 0 ? "" : drc[0]["S_QUNIANQ"].ToString();
            this.tab3txtQIANNIANQ.Text = drc.Count == 0 ? "" : drc[0]["S_QIANNIANQ"].ToString();
            this.tab3txtBENNIANQ.Text = drc.Count == 0 ? "" : drc[0]["S_BENNIANQ"].ToString();
            this.tab3txtSHANGNIANQ.Text = drc.Count == 0 ? "" : drc[0]["S_SHANGNIANQ"].ToString();
            this.tab3txtCHAOJIHTJD.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOJIHTJD"].ToString();
            this.tab3txtCHAOJIHTJZ.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOJIHTJZ"].ToString();
            this.tab3txtCHAOJIHTJSX.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOJIHTJSX"].ToString();
            this.tab3txtCHAOJIHTJG.Text = drc.Count == 0 ? "" : drc[0]["S_CHAOJIHTJG"].ToString();
            this.tab3cmbBenBuFangA.Value = drc.Count == 0 ? "" : drc[0]["S_BENBUFN"].ToString();
            this.tab3cmbBanShiCFangA.Value = drc.Count == 0 ? "" : drc[0]["S_BANSHICFN"].ToString();
            if (hasNowYear)
            {
                this.tab3lblBenBuQDFA.Value = drc[0]["S_BENBUQDFN"].ToString() == "1" ? "本部确定方案一" : drc[0]["S_BENBUQDFN"].ToString() == "2" ? "本部确定方案二" : drc[0]["S_BENBUQDFN"].ToString() == "3" ? "本部确定方案三" : "无方案";
                this.hdCollection.Set("bbqd", drc[0]["S_BENBUQDFN"]);
                this.tab3lblBanShiCQDFA.Value = drc[0]["S_BANSHICQDFN"].ToString() == "1" ? "办事处确定方案一" : drc[0]["S_BANSHICQDFN"].ToString() == "2" ? "办事处确定方案二" : drc[0]["S_BANSHICQDFN"].ToString() == "3" ? "办事处确定方案三" : "无方案";
                this.hdCollection.Set("bscqd", drc[0]["S_BANSHICQDFN"]);
            }
            //判断确定方案按钮是否可用
            DataRowCollection drcT = new YS_JH_QUEDINGJHProcess().SelectYS_JH_QUEDINGJHsDynamic("I_Y=" + (int.Parse(this.tab3txtY.Text) + 1), null).Rows;
            //this.tab3btnConfirm.Enabled = (drcT.Count == 0);
            //this.tab3btnRebuilData.Enabled = (drcT.Count == 0);
            this.tab3btnExport.Enabled = false;
            //int step = Convert.ToInt32(drc[0]["S_DAGNQIANBZ"]);
            //this.tab3btnConfirm.Enabled = (step < 2);
            //加载预测分析
            this.tab4txtY.Text = (DateTime.Now.Year+1).ToString();
            this.tab4lblBenBuFA.Value = (drc.Count == 0 ? "" : drc[0]["S_BENBUFN"].ToString()) == "1" ? "本部确定方案一" : (drc.Count == 0 ? "" : drc[0]["S_BENBUFN"].ToString()) == "2" ? "本部确定方案二" : (drc.Count == 0 ? "" : drc[0]["S_BENBUFN"].ToString()) == "3" ? "本部确定方案三" : "无方案";
            this.tab4lblBanShiCFA.Value = (drc.Count == 0 ? "" : drc[0]["S_BANSHICFN"].ToString()) == "1" ? "办事处确定方案一" : (drc.Count == 0 ? "" : drc[0]["S_BANSHICFN"].ToString()) == "2" ? "办事处确定方案二" : (drc.Count == 0 ? "" : drc[0]["S_BANSHICFN"].ToString()) == "3" ? "办事处确定方案三" : "无方案";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        //private void LoadDataTeShuYH()
        //{
            
        //    this.tab2gvList1.DataSource = _spHelper.P_YS_GETTESHUYONGHU(this.tab2cmbBZ.SelectedIndex + 1, Convert.ToInt32(this.tab2cmbY.Value), "i_y,i_bz,s_yonghuh").Tables[0]; ;
        //    this.tab2gvList1.DataBind();
        //    this.tab2lblRowCount.Text = "共有" + this.tab2gvList1.VisibleRowCount + "条记录";
        //}

        /// <summary>
        /// 数据加载
        /// </summary>
        private DataTable LoadDataYuCheFX()
        {
            this.dtYuCheFX = SearchSpHelper.P_YSJH_YUBIANZHIJHFASJTJ(int.Parse(this.tab4txtY.Text), Convert.ToInt32(this.tab4cmbType.SelectedIndex), Convert.ToInt32(this.hdCollection.Get("jjbl")), Convert.ToInt32(this.hdCollection.Get("bbxy")), Convert.ToInt32(this.hdCollection.Get("bscxy"))).Tables[0];
            this.tab4gvList1.DataSource = this.dtYuCheFX;
            this.tab4gvList1.DataBind();
            return this.dtYuCheFX;
        }

       

        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int index = ASPxPageControl1.ActiveTabIndex;
            string sName = "";
            ASPxGridView gv=null;
            DataTable dt=null;

            if (this.hdCollection.Get("type").ToString() == "tab1export")
            {
                gv = tab1gvList1;                
                dt = this.CreateDataSource("0");
                sName = "计划执行情况";          
            }
            else if (this.hdCollection.Get("type").ToString() == "tab2export")
            {
                gv = this.tab2gvList1;
                dt = this.CreateDataSource("1");
                sName = this.tab2cmbBZ.Text;
            }
            else if (this.hdCollection.Get("type").ToString() == "exportNear3yers")
            {
                
            }
            else if (this.hdCollection.Get("type").ToString() == "tab4export")
            {
                gv = this.tab4gvList1;
                dt = this.CreateDataSource("3");
                sName = this.tab4cmbType.Text;
            }
            gv.DataSource = dt;
            gv.DataBind();
            ASPxGridViewExporter1.GridViewID = gv.ID;
            gv.Settings.ShowFooter = false;

            ASPxGridViewExporter1.WriteXlsToResponse(sName);
        }

        protected void btnExport2_Click(object sender, EventArgs e)
        {

        }

        protected void btnExport3_Click(object sender, EventArgs e)
        {

        }
    }
}